/*
Copyright 2015 shizhefei（LuckyJayce）
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
   http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package com.cyw.baselibrary.view.varyview;


import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.cyw.baselibrary.R;


/**
 * 自定义要切换的布局，通过IVaryViewHelper实现真正的切换<br>
 * 使用者可以根据自己的需求，使用自己定义的布局样式
 *
 * @author LuckyJayce
 */
public class LoadViewHelper {

    private IVaryViewHelper helper;
    /**
     * 提示界面整体布局
     */
    private View emptyLayout;
    /**
     * 界面图片控件
     */
    private ImageView iv_tips;
    /**
     * 界面提示文字控件
     */
    private TextView tv_message;
    /**
     * 界面刷新按钮(可以是其他用途，如发现更多等)
     */
    private TextView btn_refresh;

    private View loadingLayout;

    public LoadViewHelper(View view) {
        this(new VaryViewHelper(view));
    }

    public LoadViewHelper(IVaryViewHelper helper) {
        super();
        this.helper = helper;
        emptyLayout = helper.inflate(R.layout.view_vary);
        loadingLayout = helper.inflate(R.layout.view_loading);
        tv_message = (TextView) emptyLayout.findViewById(R.id.tv_message);
        btn_refresh = (TextView) emptyLayout.findViewById(R.id.btn_refresh);
        iv_tips = (ImageView) emptyLayout.findViewById(R.id.iv_tips);

        emptyLayout.setOnTouchListener(onTouchListener);
        loadingLayout.setOnTouchListener(onTouchListener);


    }
    private static View.OnTouchListener onTouchListener  =  new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    };


    /**
     * 数据为空时要显示的布局
     * @param listener:按钮点击事件
     * @param message：提示消息
     * @param buttonText：按钮文本
     */
    public void showEmpty(OnClickListener listener, String message, String buttonText, int imageId) {
        iv_tips.setImageResource(imageId);
        tv_message.setText(message);
        btn_refresh.setText(buttonText);
        btn_refresh.setOnClickListener(listener);
        helper.showLayout(emptyLayout);
    }

    /**
     * 数据为空时要显示的布局（没有按钮）
     * @param message：提示消息
     */
    public void showEmptyNoButton(String message, int imageId) {
        iv_tips.setImageResource(imageId);
        tv_message.setText(message);
        btn_refresh.setVisibility(View.GONE);
        helper.showLayout(emptyLayout);
    }

    /**
     * 数据出错的布局
     * @param message：提示消息
     */
    public void showError(String message, int imageId) {
        iv_tips.setImageResource(imageId);
        tv_message.setText(message);
        btn_refresh.setVisibility(View.GONE);
        helper.showLayout(emptyLayout);
    }

    /**
     * 数据出错时要显示的布局
     * @param listener:按钮点击事件
     * @param message：提示消息
     * @param buttonText：按钮文本
     */
    public void showError(OnClickListener listener, String message, String buttonText, int imageId) {
        iv_tips.setImageResource(imageId);
        tv_message.setText(message);
        btn_refresh.setText(buttonText);
        btn_refresh.setOnClickListener(listener);
        helper.showLayout(emptyLayout);
    }

    public void showLoading(){
        helper.showLayout(loadingLayout);
    }


    public void restore() {
        helper.restoreView();
    }
}
